package com.gzh.system.controller;

import com.gzh.core.dao.po.UserEntity;
import com.gzh.core.model.vo.Result;
import com.gzh.core.service.CheckToken;
import com.gzh.core.service.LoginToken;
import com.gzh.system.model.vo.LoginParam;
import com.gzh.system.model.vo.PwdParam;
import com.gzh.system.model.vo.RegisterParam;
import com.gzh.system.service.UserService;
import com.gzh.system.service.dto.LoginOut;
import com.gzh.system.service.dto.RegisterOut;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @Author guanzh
 * @Date 2022/3/2 14:41
 * @Description
 */
@Slf4j
@RequestMapping("/v1/api/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @LoginToken
    public ResponseEntity<?> login(@Valid @RequestBody LoginParam loginParam , HttpServletResponse response) {
        LoginOut out = userService.login(loginParam.paramToIn() , response);
        return Result.ok(out);
    }

    @PostMapping("/register")
    @LoginToken
    public ResponseEntity<?> register(@RequestBody @Valid RegisterParam registerParam) {
        RegisterOut out = userService.register(registerParam.paramToIn());
        return Result.ok(out);
    }

    @PostMapping("/changeInfo")
    @CheckToken
    public ResponseEntity<?> changeInfo(@RequestBody @Valid UserEntity userEntity) {
        userService.changeInfo(userEntity);
        return Result.ok();
    }

    @PostMapping("/changePwd")
    @CheckToken
    public ResponseEntity<?> changePwd(@RequestBody @Valid PwdParam param) {
        userService.changePwd(param);
        return Result.ok();
    }

    @PostMapping("/updateImg")
    public ResponseEntity<?> test(@RequestBody UserEntity user) {
        return Result.ok(userService.updateImg(user));
    }
}
